<?php


namespace app\blog\model;


use app\common\tool\RedisClient;
use app\index\controller\Common;
use think\Model;

/**
 * 文章model
 * Class ArticleModel
 * @package app\blog\model
 */
class ArticleModel extends Model
{
    protected $name = 'article';
    /**
     * 获取多个文章的标签数据
     * @param $data
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_labels($data)
    {
        $labelModel = new LabelModel();
        foreach ($data as $key => $item) {
            $ids = explode(',', $item['label']);
            $labels = $labelModel->where('id', 'in', $ids)->field('label')->select();
            $label = '';
            for ($i = 0; $i < count($labels); $i++) {
                $label = $label . ',' . $labels[$i]['label'];
            }
            $data[$key]['label'] = substr($label, 1);
        }
        return $data;
    }

    /**
     * 获取单个文章的标签数据
     * @param $label
     * @return bool|string
     */
    public function get_label($label)
    {
        $ids = explode(',', $label);
        $labelModel = new LabelModel();
        $labels = $labelModel->where('id','in',$ids)->select();
        $label = '';
        foreach ($labels as $key => $item){
            $label .= $label . ',' . $item['label'];
        }
        $label = substr($label, 1);
        return $label;
    }

    /**
     * 获取修改文章的数据
     * @param $id
     * @return mixed
     */
    public function edit_get($id)
    {
        $data = self::get($id);
        $data['label'] = explode(',', $data['label']);
        return $data;
    }

    /**
     * 获取文章列表
     * @param $page
     * @param $limit
     * @return array|mixed|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_all_data($page, $limit)
    {
        $data = $this->field('id,title,label,add_time,click_times,status')
            ->order('id', 'desc')
            ->paginate($limit, '', ['page' => $page]);
        $data = self::get_labels($data);
        return $data->toArray();
    }

    /**
     * 获取文章列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function get_article()
    {
        $data = self::where('status', 1)
            ->order('add_time', 'desc')
            ->field(['id', 'title', 'add_time', 'click_times'])
            ->paginate(100, ['query' => request()->param()]);
        return $data;
    }

    /**
     * 文章被点击，阅读次数加1
     * @param $id
     */
    public static function click_once($id)
    {
        $res = self::get($id);
        $res->click_times = $res->click_times + 1;
        $res->save();
    }

    /**
     * 数据已经丢失，重新读取，再存放至缓存中
     * @param $id
     * @return mixed
     */
    public static function read($id)
    {
        $data = self::get($id);
        $ids = explode(',', $data['label']);
        $labels = LabelModel::all($ids);
        $label = '';
        for ($i = 0; $i < count($labels); $i++) {
            $label = $label . ',' . $labels[$i]['label'];
        }
        $data['label'] = substr($label, 1);
        $article = json_encode($data);
        $redis = RedisClient::get_redis();
        $redis->set('article_' . $id, $article);
        return $data;
    }

}